﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.Sql;
using System.Data.SqlClient;

namespace KetNoiCSDL
{
    public class KNCoSoDuLieu
    {
        protected SqlConnection Connect;
        protected SqlDataAdapter adapter;
        protected SqlCommand cmd;
        protected static string _ConStr =
                    "Server=(local);Database=DB_QLMayMac;Integrated Security=SSPI;";
        public static string ConnectionString
        {
            get{ return _ConStr; }
            set{ _ConStr = value; }
        }
       
        public void connect()
        {
            if (_ConStr != null)
            {
                Connect = new SqlConnection(_ConStr);
                Connect.Open();
            } 
            else
                throw new Exception("Không kết nối được dữ liệu.");
        }

        public void disconnect()
        {
            Connect.Close();
        }

        public DataSet executeQueryDs(string sqlString)
        {
            DataSet ds = new DataSet();
            adapter = new SqlDataAdapter(sqlString, Connect);
            adapter.Fill(ds);
            return ds;
        }
        public DataTable executeQuery(string sqlString)
        {
            DataSet ds = new DataSet();
            adapter = new SqlDataAdapter(sqlString, Connect);
            adapter.Fill(ds);
            return ds.Tables[0];
        }
        public void executeNonQuery(string sqlString)
        {
            cmd = new SqlCommand(sqlString, Connect);
            cmd.ExecuteNonQuery();
        }

        public object executeScalar(string sqlString)
        {
            cmd = new SqlCommand(sqlString, Connect);
            return cmd.ExecuteScalar();
        }
    }
}
 